﻿using ConcreteService;
using Infrastructures;
using Models;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

public class FileUtil
{
    public static string SaveFile(HttpPostedFile fu, FileType type, int belongNO, string userid, APPRole role)
    {
        UtilService utilService = ServiceFactory.CreateInstance<UtilService>();
        IList<FileSystem> lst = utilService.ListFileSystem(type, belongNO);
        string[] ext = { ".jpg", ".png", ".jpeg", ".doc", ".docx" };

        if (fu.ContentLength > 1000000)
            throw new Exception("文件长度不能大于1M");

        string fileName = fu.FileName;

        string fileExt = Path.GetExtension(fileName);

        if (!ext.Contains(fileExt.ToLower()))
        {
            throw new Exception("只能上传jpg,png,jpeg,doc,docx格式");
        }

        string strDir = SystemConfig.Get("UploadPath") + "\\" + role.ToString() + "\\" + userid + "\\" + belongNO + "\\";

        if (!Directory.Exists(strDir))
            Directory.CreateDirectory(strDir);

        Random don = new Random();

        string strSysName = type.ToString() + DateTime.Now.ToString("yyyyMMddHHmmss") + don.Next(100, 1000) + fileExt;
        if (type == FileType.AssoLogo || type == FileType.CompanyLogo || type == FileType.AgentLogo)
        {
            using (Image image = Image.FromStream(fu.InputStream).GetThumbnailImage(200, 200, () => { return false; }, IntPtr.Zero))
            {
                image.Save(strDir + strSysName);
            }
        }
        else
        {
            fu.SaveAs(strDir + strSysName);
        }

        FileSystem fs = null;

        if (lst != null && lst.Count > 0)
            fs = lst.FirstOrDefault(x => x.Fun == type);
        else
            fs = new FileSystem();

        fs.Fun = type;
        fs.TypeName = role;
        fs.BelongNO = belongNO;
        fs.NewName = strSysName;
        fs.OldName = fileName;
        fs.Path = role.ToString() + "/" + userid + "/" + belongNO + "/" + strSysName;
        fs.Status = CommonStatus.Active;
        fs.InstanceID = SystemConfig.Get("InstanceID");
        fs.CreateUser = userid.ToInt();
        fs.CtreateTime = DateTime.Now;

        if (lst == null || lst.Count == 0)
            utilService.CreateFileSystem(fs, userid);
        else
            utilService.UpdateFileSystem(fs, userid);

        return fs.Path;
    }

    public static void SaveFile(HttpPostedFile fu, FileType type, IList<FileSystem> lst, int projectNo, string userid, Image img = null, bool saveOrgFile = true)
    {
        UtilService utilService = ServiceFactory.CreateInstance<UtilService>();
        ProjectService service = ServiceFactory.CreateInstance<ProjectService>();

        string fileName = fu.FileName;
        string fileExt = Path.GetExtension(fileName);
        string[] ext = { ".jpg", ".png", ".jpeg", ".doc", ".docx", ".xlsx", ".xls", ".gif", };

        if (type == FileType.SponsorBook)
        {
            if (fu.ContentLength > 8000000)
                throw new Exception("文件长度不能大于8M");
        }
        else
        {
            if (fu.ContentLength > 1000000)
                throw new Exception("文件长度不能大于1M");
        }

        if (!ext.Contains(fileExt.ToLower()))
        {
            throw new Exception("只能上传jpg,png,jpeg,doc,docx格式");
        }

        string strDir = SystemConfig.Get("UploadPath") + "\\Project\\" + userid + "\\" + projectNo + "\\";

        if (!Directory.Exists(strDir))
            Directory.CreateDirectory(strDir);

        Random don = new Random();

        string strSysName = type.ToString() + "_" + DateTime.Now.ToString("yyyyMMddHHmmss") + don.Next(100, 1000) + fileExt;
        string orgName = fileName.Substring(0, fileName.Length - fileExt.Length - 1);

        if (img != null)
        {
            img.Save(strDir + strSysName, ImageFormat.Jpeg);
            if (saveOrgFile)
                fu.SaveAs(strDir + orgName + "_" + DateTime.Now.ToString("yyyyMMddHHmmss") + don.Next(100, 1000) + fileExt);
        }
        else
            fu.SaveAs(strDir + strSysName);

        FileSystem fs = null;

        if (lst != null && lst.Count > 0)
            fs = lst.FirstOrDefault(x => x.Fun == type);
        else
            fs = new FileSystem();

        fs.Fun = type;
        fs.TypeName = APPRole.Project;
        fs.BelongNO = projectNo;
        fs.NewName = strSysName;
        fs.OldName = fileName;
        fs.Path = "Project/" + userid + "/" + projectNo + "/" + strSysName;
        fs.Status = CommonStatus.Active;
        fs.InstanceID = SystemConfig.Get("InstanceID");
        fs.CreateUser = userid.ToInt();
        fs.CtreateTime = DateTime.Now;

        if (type == FileType.ActCoverThumb)
        {
            Project pro = service.Get<Project>(projectNo.ToString());
            pro.Cover = fs.Path;
            service.Update<Project>(pro, userid);
        }

        if (lst == null || lst.Count == 0)
            utilService.CreateFileSystem(fs, userid);
        else
            utilService.UpdateFileSystem(fs, userid);
    }
}
